package com.ziyun.pop.mall.module.time.service;

import com.utility.DateUtils;
import com.utility.JsonUtils;
import com.utility.New;
import com.utility.StringUtils;
import com.ziyun.pop.mall.base.utils.SerializeUtil;
import com.ziyun.pop.mall.module.time.action.UserActionTake;
import com.ziyun.pop.mall.module.time.dao.PopUserActionDao;
import com.ziyun.pop.mall.module.user.entity.PopUserAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:chenxiaoyi
 * @Date:2018/11/6 14:02
 */
@Service
@Transactional
public class PopUserActionService {

    private static Logger _log = LoggerFactory.getLogger(PopUserActionService.class);


    @Autowired
    private PopUserActionDao popUserActionDao;

    /**
     * 定时插入用户记录
     */
    @Transactional
    public synchronized void  insertUserAction(byte[] bytes){
        long startTime = System.currentTimeMillis();
        Map<String, PopUserAction> actionMap = (Map<String, PopUserAction>)SerializeUtil.deserializeToObject(bytes);

        List<PopUserAction> popUserActions = popUserActionDao.selectAll();//表里所有的记录
        List<PopUserAction> list = New.arrayList();//当天所有的链接和数量纪录
        for (String url :actionMap.keySet()) {
            PopUserAction action = actionMap.get(url);
            action.setClickNum(action.getAtomicLong().longValue());
            list.add(action);
        }

        if(popUserActions.isEmpty()){
            popUserActionDao.insertList(list);
        }else{
            List<PopUserAction> insertList = New.arrayList();//新增数据
            List<PopUserAction> updateList = New.arrayList();//修改数据

            for (PopUserAction v:list) {
                v.setClickNum(v.getAtomicLong().longValue());
                for (PopUserAction p:popUserActions) {
                    if(StringUtils.equals(p.getUrl(),v.getUrl())){
                        v.setClickNum(v.getClickNum()+p.getClickNum()); //累加数量
                        v.setId(p.getId()); //按照id 批量修改
                        updateList.add(v);
                    }
                }
            }

            List<PopUserAction> collect = list.stream().filter(popUserAction ->  null==popUserAction.getId()).collect(Collectors.toList());
            insertList.addAll(collect);

            if(!updateList.isEmpty()){
                popUserActionDao.updateList(updateList);
            }
            if(!insertList.isEmpty()){
                popUserActionDao.insertList(insertList);
            }
        }
        long endTime = System.currentTimeMillis();        // 打印耗时
        String start = DateUtils.formatDefault(startTime);
        String end = DateUtils.formatDefault(endTime);
        long cost = endTime - startTime;
        _log.info("用户行为记录定时任务开始时间 [{}],结束时间 [{}],耗时 [{}] ", start, end, cost);
    }
}
